package by.bsu.scs.presentation.form;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.validator.ValidatorForm;

import by.bsu.scs.security.MD5Encoder;

/**
 * @author Alexey Hursevich
 * @version 1.0
 * 
 * Profile form bean.
 */
public class ProfileForm extends ValidatorForm {
	private static final long serialVersionUID = 1L;
	
	private static final String METHOD_POST = "post";
	
	private String currentLogin;
	private String currentPassword;
	private String oldPasswordCheck;
	private String newPassword;
	private String newPasswordConfirm;

	/**
	 * Verifies that user entered correct current password to 
	 * change it.
	 * 
	 * @param mapping
	 * @param request
	 * 
	 * @return errors
	 */
	@Override
	public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {
		
		ActionErrors errors = new ActionErrors();

		if( METHOD_POST.equalsIgnoreCase( request.getMethod() ) ) {
			oldPasswordCheck = MD5Encoder.getHash( oldPasswordCheck );
			
			if( !currentPassword.equalsIgnoreCase( oldPasswordCheck ) ) {
				errors.add("oldPasswordCheck", new ActionMessage("error.wrongOldPassword"));
			}
		}
		
		return errors;
	}
	
	public String getCurrentLogin() {
		return currentLogin;
	}
	public void setCurrentLogin(String currentLogin) {
		this.currentLogin = currentLogin;
	}
	public String getCurrentPassword() {
		return currentPassword;
	}
	public void setCurrentPassword(String currentPassword) {
		this.currentPassword = currentPassword;
	}
	public String getNewPassword() {
		return newPassword;
	}
	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}
	public String getNewPasswordConfirm() {
		return newPasswordConfirm;
	}
	public void setNewPasswordConfirm(String newPasswordConfirm) {
		this.newPasswordConfirm = newPasswordConfirm;
	}	
	public String getOldPasswordCheck() {
		return oldPasswordCheck;
	}
	public void setOldPasswordCheck(String oldPasswordCheck) {
		this.oldPasswordCheck = oldPasswordCheck;
	}
}
